package com.example.excelapache_poi.controller;
import com.example.excelapache_poi.common.ExceptionUtil;
import com.example.excelapache_poi.common.Result;
import com.example.excelapache_poi.common.ResultException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/07/06  13:11
 * @Description:
 */
public class BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

    /**
     * 处理异常
     * @param e
     * @param errMsg 指定返回给前端的信息
     * @return
     */
    protected Result handleException(Throwable e, String errMsg) {
        if (e instanceof ResultException) {
            return ((ResultException) e).getResult();
        }
        LOGGER.warn(ExceptionUtil.getDetail(e));
        if (StringUtils.isNotEmpty(errMsg)) {
            return Result.failWithMsg(errMsg);
        }
        return Result.serverError();
    }

    /**
     * 处理异常
     * @param e
     * @return
     */
    protected Result handleException(Throwable e) {
        if (e instanceof ResultException) {
            return ((ResultException) e).getResult();
        }
        LOGGER.error(ExceptionUtil.getDetail(e));
        return Result.serverError();
    }
}